import Cookie from "js-cookie";
export default {
  namespaced: true,
  state: {
    token: "",
    username: "",
    userPermission: "",
  },
  mutations: {
    setToken(state, val) {
      state.token = val;
      Cookie.set("token", val);
    },
    clearToken(state) {
      state.token = "";
      Cookie.remove("token");
    },
    getToken(state) {
      state.token = Cookie.get("token") || state.token;
    },
    setUsername(state, val) {
      state.username = val;
      state.userPermission = val === "admin" ? "超级管理员" : "普通用户";
      Cookie.set(
        "username",
        JSON.stringify({
          username: state.username,
          userPermission: state.userPermission,
        })
      );
    },
    getUsername(state) {
      const userObj = JSON.parse(Cookie.get("username"));
      state.username = userObj.username || state.token;
      state.userPermission = userObj.userPermission;
    },
  },
};
